package com.cn.yz.springboot.util.tablesplit;

import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StrategyManager {

	private Logger logger = LoggerFactory.getLogger(StrategyManager.class);
	
	private Map<String,Strategy> strategies = new ConcurrentHashMap<String,Strategy>(10);//TODO:?
	
	public Strategy getStrategy(String key) {
		return strategies.get(key);
	}
	
	public Map<String,Strategy> getStrategies(){
		return strategies;
	}
	
	public  void setStrategies(Map<String, String> strategies) {
		for(Entry<String, String> entry : strategies.entrySet()){
			try {
				this.strategies.put(entry.getKey(),(Strategy)Class.forName(entry.getValue()).newInstance());
			} catch (Exception e) {
				logger.error("实例化策略出错", e);
			}
		}
		printDebugInfo();
	}

	private void printDebugInfo(){
		StringBuffer msg= new StringBuffer("初始化了"+strategies.size()+"策略");
		for(String key: strategies.keySet()){
			msg.append("\n");
			msg.append(key);
			msg.append("  --->  ");
			msg.append(strategies.get(key));
		}
		logger.debug(msg.toString());
	}
}
